// Bitmap.h

#pragma once
#ifndef BITMAP_H_INCLUDED
#define BITMAP_H_INCLUDED

class GDIFont
{
public:
	/// A single kerning pair
	struct KerningPair
	{
		word m_uFirst;		///< First character
		word m_uSecond;		///< Second character
		int32 m_iOffset;	///< Pixel offset
	};

	void*			m_hFont;			///< Win32 font handle
	KerningPair*	m_axKerning;		///< List of kerning pairs
	int				m_iKerningCnt;		///< Number of kerning pairs

	GDIFont();
	~GDIFont();
	GDIFont(const GDIFont& p_xrA);
	GDIFont& operator=(const GDIFont& p_xrA);

	bool Init(const char* p_sFaceName,int p_iPixelHeight,int p_iWeight=400,int p_iCharSet=1);
	void Free();

	int GetKerning(dword p_uFirst,dword p_uSecond) const;
};

class Bitmap
{
public:

	int m_iW,m_iH;
	DWORD* m_pBM;
	HBITMAP m_hDIB;

	Bitmap();
	~Bitmap();

	void Init(int p_iWidth,int p_iHeight);
	void Shut();

	void Clear(DWORD p_dwColor);
	void SetPixel(int p_iX,int p_iY,DWORD p_dwColor);

	static void ColorUnpack(DWORD p_dwColor,int& po_iR,int& po_iG,int& po_iB);
	static DWORD ColorPack(int p_iR,int p_iG,int p_iB);

	bool LoadTGA(const char* p_sFilename);
	bool SaveTGA(const char* p_sFilename);

	static void PixelFormatConversion(void* p_pSrc,dword p_uSR,dword p_uSG,dword p_uSB,dword p_uSA,
		void* p_pDst,dword p_uDR,dword p_uDG,dword p_uDB,dword p_uDA,int p_iNumPixels);

	void DrawLine(int p_iX0,int p_iY0,int p_iX1,int p_iY1,DWORD p_dwColor);
	void DrawCircle(int p_iX0,int p_iY0,int p_iR,DWORD p_dwColor);
	void FillCircle(int p_iX0,int p_iY0,int p_iR,DWORD p_dwColor);

	void DrawLineIpol(int p_iX0,int p_iY0,DWORD p_dwColor0,int p_iX1,int p_iY1,DWORD p_dwColor1);


	void DrawSpan(int p_iX0,int p_iX1,int p_iY,DWORD p_dwColor0,DWORD p_dwColor1);

	bool DrawTriEdge(
		int p_iX0,int p_iY0,DWORD p_dwColor0,
		int p_iX1,int p_iY1,DWORD p_dwColor1,
		int p_iX2,int p_iY2,DWORD p_dwColor2
		);

	bool DrawTri(
		int p_iX0,int p_iY0,DWORD p_dwColor0,
		int p_iX1,int p_iY1,DWORD p_dwColor1,
		int p_iX2,int p_iY2,DWORD p_dwColor2
		);

	void DrawTextA(int p_iX,int p_iY,const char* sText,dword p_uColor);
	void DrawTextA(int p_iX,int p_iY,const char* sText,dword p_uColor,const GDIFont& p_rxFont);

};

#endif // BITMAP_H_INCLUDED
